/*
 * All content copyright http://www.j2eefast.com, unless
 * otherwise indicated. All rights reserved.
 * No deletion without permission
 */
package com.j2eefast.web.service;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.j2eefast.web.common.BaseUrl;
import com.j2eefast.web.entity.ShopEntity;
import com.j2eefast.web.mapper.ShopMapper;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.j2eefast.common.core.utils.ToolUtil;
import com.j2eefast.framework.utils.Constant;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import java.util.Map;
import java.util.List;
import java.util.Arrays;

/**
 * 【请填写功能名称】Service接口
 * @author: ZhouZhou
 * @date 2021-06-11 10:29:56
 */
@Service
public class ShopService extends ServiceImpl<ShopMapper,ShopEntity> {

	@Resource
	private ShopMapper shopMapper;

	public void saveShop(Long shopId){
		String url = String.format(BaseUrl.SHOP_DETAIL,shopId);
		String res = HttpUtil.get(url);

		JSONObject data = JSONUtil.parseObj(res);
		if(data.getInt("code").equals(101)){
			saveData(data.getJSONObject("data"));
		}
	}

	private void saveData(JSONObject data) {
		ShopEntity shop = JSONUtil.toBean(data,ShopEntity.class);

		ShopEntity query = new ShopEntity();
		query.setId(shop.getId());
		List<ShopEntity> dataList = shopMapper.findList(query);
		if(dataList != null && dataList.size() > 0){
			shop.setPkId(dataList.get(0).getPkId());
		}

		this.saveOrUpdate(shop);
	}

	/**
	 * mybaitis-plus 单表页面分页查询
     */
	public PageUtil findPage(Map<String, Object> params) {
		QueryWrapper<ShopEntity> queryWrapper = new QueryWrapper<ShopEntity>();
		String aboutUrl = (String) params.get("aboutUrl");
        queryWrapper.eq(ToolUtil.isNotEmpty(aboutUrl), "about_url", aboutUrl);
		String accountPeriod = (String) params.get("accountPeriod");
        queryWrapper.eq(ToolUtil.isNotEmpty(accountPeriod), "account_period", accountPeriod);
		String addTime = (String) params.get("addTime");
        queryWrapper.eq(ToolUtil.isNotEmpty(addTime), "add_time", addTime);
		String area = (String) params.get("area");
        queryWrapper.eq(ToolUtil.isNotEmpty(area), "area", area);
		String areaStr = (String) params.get("areaStr");
        queryWrapper.eq(ToolUtil.isNotEmpty(areaStr), "area_str", areaStr);
		String bankAccountName = (String) params.get("bankAccountName");
        queryWrapper.like(ToolUtil.isNotEmpty(bankAccountName), "bank_account_name", bankAccountName);
		String bankName = (String) params.get("bankName");
        queryWrapper.like(ToolUtil.isNotEmpty(bankName), "bank_name", bankName);
		String bankNum = (String) params.get("bankNum");
        queryWrapper.eq(ToolUtil.isNotEmpty(bankNum), "bank_num", bankNum);
		String businessScope = (String) params.get("businessScope");
        queryWrapper.eq(ToolUtil.isNotEmpty(businessScope), "business_scope", businessScope);
		String city = (String) params.get("city");
        queryWrapper.eq(ToolUtil.isNotEmpty(city), "city", city);
		String company = (String) params.get("company");
        queryWrapper.eq(ToolUtil.isNotEmpty(company), "company", company);
		String companyAddress = (String) params.get("companyAddress");
        queryWrapper.eq(ToolUtil.isNotEmpty(companyAddress), "company_address", companyAddress);
		String contactCell = (String) params.get("contactCell");
        queryWrapper.eq(ToolUtil.isNotEmpty(contactCell), "contact_cell", contactCell);
		String contactName = (String) params.get("contactName");
        queryWrapper.like(ToolUtil.isNotEmpty(contactName), "contact_name", contactName);
		String contactUrl = (String) params.get("contactUrl");
        queryWrapper.eq(ToolUtil.isNotEmpty(contactUrl), "contact_url", contactUrl);
		String deleted = (String) params.get("deleted");
        queryWrapper.eq(ToolUtil.isNotEmpty(deleted), "deleted", deleted);
		String headImg = (String) params.get("headImg");
        queryWrapper.eq(ToolUtil.isNotEmpty(headImg), "head_img", headImg);
		String homeOrAbroad = (String) params.get("homeOrAbroad");
        queryWrapper.eq(ToolUtil.isNotEmpty(homeOrAbroad), "home_or_abroad", homeOrAbroad);
		String iDcardUrl = (String) params.get("iDcardUrl");
        queryWrapper.eq(ToolUtil.isNotEmpty(iDcardUrl), "i_dcard_url", iDcardUrl);
		String iDcardUrl2 = (String) params.get("iDcardUrl2");
        queryWrapper.eq(ToolUtil.isNotEmpty(iDcardUrl2), "i_dcard_url2", iDcardUrl2);
		String isSpecial = (String) params.get("isSpecial");
        queryWrapper.eq(ToolUtil.isNotEmpty(isSpecial), "is_special", isSpecial);
		String legalPerson = (String) params.get("legalPerson");
        queryWrapper.eq(ToolUtil.isNotEmpty(legalPerson), "legal_person", legalPerson);
		String licenseNum = (String) params.get("licenseNum");
        queryWrapper.eq(ToolUtil.isNotEmpty(licenseNum), "license_num", licenseNum);
		String licenseUrl = (String) params.get("licenseUrl");
        queryWrapper.eq(ToolUtil.isNotEmpty(licenseUrl), "license_url", licenseUrl);
		String logo = (String) params.get("logo");
        queryWrapper.eq(ToolUtil.isNotEmpty(logo), "logo", logo);
		String logoUrl = (String) params.get("logoUrl");
        queryWrapper.eq(ToolUtil.isNotEmpty(logoUrl), "logo_url", logoUrl);
		String materials = (String) params.get("materials");
        queryWrapper.eq(ToolUtil.isNotEmpty(materials), "materials", materials);
		String name = (String) params.get("name");
        queryWrapper.like(ToolUtil.isNotEmpty(name), "name", name);
		String orgId = (String) params.get("orgId");
        queryWrapper.eq(ToolUtil.isNotEmpty(orgId), "org_id", orgId);
		String percentPay = (String) params.get("percentPay");
        queryWrapper.eq(ToolUtil.isNotEmpty(percentPay), "percent_pay", percentPay);
		String province = (String) params.get("province");
        queryWrapper.eq(ToolUtil.isNotEmpty(province), "province", province);
		String saleArea = (String) params.get("saleArea");
        queryWrapper.eq(ToolUtil.isNotEmpty(saleArea), "sale_area", saleArea);
		String servArea = (String) params.get("servArea");
        queryWrapper.eq(ToolUtil.isNotEmpty(servArea), "serv_area", servArea);
		String servCity = (String) params.get("servCity");
        queryWrapper.eq(ToolUtil.isNotEmpty(servCity), "serv_city", servCity);
		String servProvince = (String) params.get("servProvince");
        queryWrapper.eq(ToolUtil.isNotEmpty(servProvince), "serv_province", servProvince);
		String serviceArea = (String) params.get("serviceArea");
        queryWrapper.eq(ToolUtil.isNotEmpty(serviceArea), "service_area", serviceArea);
		String serviceHotline = (String) params.get("serviceHotline");
        queryWrapper.eq(ToolUtil.isNotEmpty(serviceHotline), "service_hotline", serviceHotline);
		String shopNature = (String) params.get("shopNature");
        queryWrapper.eq(ToolUtil.isNotEmpty(shopNature), "shop_nature", shopNature);
		String spuSort = (String) params.get("spuSort");
        queryWrapper.eq(ToolUtil.isNotEmpty(spuSort), "spu_sort", spuSort);
		String status = (String) params.get("status");
        queryWrapper.eq(ToolUtil.isNotEmpty(status), "status", status);
		String summary = (String) params.get("summary");
        queryWrapper.eq(ToolUtil.isNotEmpty(summary), "summary", summary);
		String taxNum = (String) params.get("taxNum");
        queryWrapper.eq(ToolUtil.isNotEmpty(taxNum), "tax_num", taxNum);
		String taxStatus = (String) params.get("taxStatus");
        queryWrapper.eq(ToolUtil.isNotEmpty(taxStatus), "tax_status", taxStatus);
		String taxUrl = (String) params.get("taxUrl");
        queryWrapper.eq(ToolUtil.isNotEmpty(taxUrl), "tax_url", taxUrl);
		String userId = (String) params.get("userId");
        queryWrapper.eq(ToolUtil.isNotEmpty(userId), "user_id", userId);
		Page<ShopEntity> page = shopMapper.selectPage(new Query<ShopEntity>(params).getPage(), queryWrapper);
		return new PageUtil(page);
    }

	/**
	 * 自定义分页查询，含关联实体对像
	 */
	public PageUtil findPage(Map<String, Object> params,ShopEntity shopEntity) {
		Page<ShopEntity> page = shopMapper.findPage(new Query<ShopEntity>(params).getPage(),
					shopEntity,
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}

	/**
	* 查列表
	*/
	public List<ShopEntity> findList(ShopEntity shopEntity){
		return shopMapper.findList(shopEntity);
	}

	/**
     * 批量删除
     */
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteBatchByIds(Long[] ids) {
		return this.removeByIds(Arrays.asList(ids));
	}

	/**
     * 单个删除
     */
	@Transactional(rollbackFor = Exception.class)
	public boolean delShopById(Long pkId) {
		return this.removeById(pkId);
	}

	/**
     * 保存
     */
	@Transactional(rollbackFor = Exception.class)
	public boolean addShop(ShopEntity shop){
		if(this.save(shop)){
			return true;
		}
        return false;
    }

	/**
     * 修改根居ID
     */
	@Transactional(rollbackFor = Exception.class)
	public boolean updateShopById(ShopEntity shop) {
		if(this.updateById(shop)){
			return true;
		}
		return false;
	}

	/**
     * 根居ID获取对象
     */
	public ShopEntity findShopById(Long pkId){
		return shopMapper.findShopById(pkId);
	}


}
